/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.visitrecord.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.knet.sp.ncrm.common.config.Global;
import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.customer.visitrecord.entity.VisitRecord;
import cn.knet.sp.ncrm.modules.customer.visitrecord.service.VisitRecordService;
import cn.knet.sp.ncrm.modules.customer.visitrecord.vo.VisitRecordVo;

/**
 * 销售跟踪Controller
 * @author 冯亚鹏
 * @version 2015-08-28
 */
@Controller
@RequestMapping(value = "${adminPath}/customer/visitRecord")
public class VisitRecordController extends BaseController {

	@Autowired
	private VisitRecordService visitRecordService;
	
	@ModelAttribute
	public VisitRecord get(@RequestParam(required=false) String id) {
		VisitRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = visitRecordService.get(id);
		}
		if (entity == null){
			entity = new VisitRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("customer:visitRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(VisitRecord visitRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<VisitRecord> page = visitRecordService.findPage(new Page<VisitRecord>(request, response), visitRecord); 
		model.addAttribute("page", page);
		return "modules/customer/visitrecord/visitRecordAjaxList";//ajax模式
		//return "modules/customer/visitrecord/visitRecordList";//form表单模式
	}

	@RequiresPermissions("customer:visitRecord:view")
	@RequestMapping(value = "listAjaxData")
	@ResponseBody
	public Page listAjaxData(VisitRecord visitRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<VisitRecord> page = visitRecordService.findPage(new Page<VisitRecord>(request, response), visitRecord);
		return page;
	}

	@RequiresPermissions("customer:visitRecord:view")
	@RequestMapping(value = "form")
	public String form(VisitRecord visitRecord, Model model) {
	    visitRecord.addHistoryFlag();
		model.addAttribute("visitRecord", visitRecord);
		return "modules/customer/visitrecord/visitRecordForm";
	}

	@RequiresPermissions("customer:visitRecord:edit")
	@RequestMapping(value = "save")
	public String save(VisitRecord visitRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, visitRecord)){
			return form(visitRecord, model);
		}
		visitRecordService.save(visitRecord);
		addMessage(redirectAttributes, "保存跟踪记录成功");
		return "redirect:"+Global.getAdminPath()+"/customer/visitRecord/?repage";
	}

	//ajax模式 删除
	@RequiresPermissions("customer:visitRecord:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(VisitRecord visitRecord, RedirectAttributes redirectAttributes) {
		AjaxMessage result = AjaxMessage.success();
		try {
			visitRecordService.delete(visitRecord);
			result.appendMessage("删除跟踪记录成功!");
		} catch (Exception e) {
			logger.warn("删除跟踪记录失败", e);
			result.makeError("删除跟踪记录失败!系统发生异常");
		}
		return result;
	}

    @RequestMapping("listVisiData")
	@ResponseBody
	public Page<VisitRecordVo> listVisiData(VisitRecord visitRecord){
		Page<VisitRecordVo> page = new Page();

		page.setList(visitRecordService.findVoList(visitRecord));
		return page;
	}


}